package com.UI
{
	import com.*;
	import com.Engine.*;
	import com.Packet.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.external.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.system.*;
	import flash.text.*;
	import flash.ui.*;
	import flash.utils.*;
	
	import gs.*;
	import gs.easing.*;
	import gs.events.TweenEvent;
	
	import org.aswing.*;
	import org.aswing.border.*;
	import org.aswing.border.EmptyBorder;
	import org.aswing.colorchooser.*;
	import org.aswing.dnd.*;
	import org.aswing.event.*;
	import org.aswing.ext.*;
	import org.aswing.geom.*;
	import org.aswing.tree.AbstractLayoutCache;
	
	public class FrameTrends extends JFrame
	{
		//members define
		private var panel6:JPanel;
		private var panel114:JPanel;
		private var btn_trends_tab_all:JToggleButton;
		private var btn_trends_tab_home:JToggleButton;
		private var btn_trends_tab_slave:JToggleButton;
		private var btn_trends_tab_lover:JToggleButton;
		private var btn_trends_tab_friend:JToggleButton;
		private var btn_trends_tab_world:JToggleButton;
		private var scrollpane:JScrollPane;
		private var txtTrends:JTextArea;
		//-----------------------------------
		private var btn_close:JButton;
		public function FrameTrends()
		{
			super();
			//setClosable(false);
			setResizable(false);  
			setBorder(new EmptyBorder());
			btn_close = new JButton();
			btn_close.setLocation(new IntPoint(448, 0));
			btn_close.setSize(new IntDimension(27, 27));
			btn_close.setPreferredSize(new IntDimension(27, 27));
			this.getTitleBar().setCloseButton(btn_close);
			getTitleBar().setMinimizeHeight(32);
			
			//component creation
			setSize(new IntDimension(514, 424));
			setPreferredSize(new IntDimension(514, 424));
			var layout0:EmptyLayout = new EmptyLayout();
			getContentPane().setLayout(layout0);
			
			panel6 = new JPanel();
			panel6.setLocation(new IntPoint(0, 0));
			panel6.setSize(new IntDimension(514, 33));
			panel6.setPreferredSize(new IntDimension(514, 33));
			var layout1:FlowWrapLayout = new FlowWrapLayout();
			layout1.setPreferWidth(200);
			layout1.setAlignment(AsWingConstants.LEFT);
			layout1.setHgap(20);
			layout1.setVgap(6);
			layout1.setMargin(true);
			panel6.setLayout(layout1);
			
			panel114 = new JPanel();
			panel114.setLocation(new IntPoint(0, 0));
			panel114.setSize(new IntDimension(350, 23));
			panel114.setPreferredSize(new IntDimension(350, 23));
			var layout2:FlowLayout = new FlowLayout();
			layout2.setAlignment(AsWingConstants.LEFT);
			layout2.setHgap(3);
			layout2.setVgap(0);
			layout2.setMargin(false);
			panel114.setLayout(layout2);
			
			btn_trends_tab_all = new JToggleButton();
			btn_trends_tab_all.setLocation(new IntPoint(0, 1));
			btn_trends_tab_all.setSize(new IntDimension(55, 23));
			btn_trends_tab_all.setPreferredSize(new IntDimension(55, 23));
			btn_trends_tab_all.setText("label");
			
			btn_trends_tab_home = new JToggleButton();
			btn_trends_tab_home.setLocation(new IntPoint(174, 0));
			btn_trends_tab_home.setSize(new IntDimension(31, 23));
			btn_trends_tab_home.setPreferredSize(new IntDimension(55, 23));
			btn_trends_tab_home.setText("label");
			
			btn_trends_tab_slave = new JToggleButton();
			btn_trends_tab_slave.setLocation(new IntPoint(58, 23));
			btn_trends_tab_slave.setSize(new IntDimension(31, 23));
			btn_trends_tab_slave.setPreferredSize(new IntDimension(55, 23));
			btn_trends_tab_slave.setText("label");
			
			btn_trends_tab_lover = new JToggleButton();
			btn_trends_tab_lover.setLocation(new IntPoint(116, 23));
			btn_trends_tab_lover.setSize(new IntDimension(55, 23));
			btn_trends_tab_lover.setPreferredSize(new IntDimension(55, 23));
			btn_trends_tab_lover.setText("label");
			
			btn_trends_tab_friend = new JToggleButton();
			btn_trends_tab_friend.setLocation(new IntPoint(58, 23));
			btn_trends_tab_friend.setSize(new IntDimension(55, 23));
			btn_trends_tab_friend.setPreferredSize(new IntDimension(55, 23));
			btn_trends_tab_friend.setText("label");
			
			btn_trends_tab_world = new JToggleButton();
			btn_trends_tab_world.setLocation(new IntPoint(116, 23));
			btn_trends_tab_world.setSize(new IntDimension(55, 23));
			btn_trends_tab_world.setPreferredSize(new IntDimension(55, 23));
			btn_trends_tab_world.setText("label");
			
			scrollpane = new JScrollPane();
			scrollpane.setLocation(new IntPoint(9, 36));
			scrollpane.setSize(new IntDimension(496, 373));
			scrollpane.setPreferredSize(new IntDimension(496, 373));
			
			txtTrends = new JTextArea();
			txtTrends.setLocation(new IntPoint(9, 36));
			txtTrends.setSize(new IntDimension(496, 373));
			txtTrends.setPreferredSize(new IntDimension(496, 373));
			txtTrends.setConstraints("Center");
			txtTrends.setText("暂未开放");
			txtTrends.setEditable(false);
			txtTrends.setWordWrap(true);
			
			//component layoution
			getContentPane().append(panel6);
			getContentPane().append(scrollpane);
			
			panel6.append(panel114);
			
			panel114.append(btn_trends_tab_all);
			panel114.append(btn_trends_tab_home);
			panel114.append(btn_trends_tab_slave);
			panel114.append(btn_trends_tab_lover);
			panel114.append(btn_trends_tab_friend);
			panel114.append(btn_trends_tab_world);
			
			scrollpane.append(txtTrends);
			//=---------------------------
			//this.getTitleBar().setMinimizeHeight(20);
			this.setClipMasked(false);
			this.getContentPane().setClipMasked(false);
			setImages();
		}
		public function setImages():void
		{
			var img_background:DisplayObject = Client.getUIElement("img_trends_bg") as DisplayObject
			setBackgroundDecorator(new AssetBackground(img_background));
			//setSizeWH(img_background.width,img_background.height);
			setSize(new IntDimension(img_background.width, img_background.height));
			setPreferredSize(new IntDimension(img_background.width, img_background.height));
			setLocationXY((Engine.W - width) / 2,(Engine.H - height)/2);
			
			panel114.append(btn_trends_tab_all);
			panel114.append(btn_trends_tab_home);
			panel114.append(btn_trends_tab_slave);
			panel114.append(btn_trends_tab_lover);
			panel114.append(btn_trends_tab_friend);
			panel114.append(btn_trends_tab_world);
			
			btn_trends_tab_all.setSelected(true);
			btn_trends_tab_all.wrapSimpleButton(Client.getUIElement("btn_trends_tab_all") as SimpleButton);
			btn_trends_tab_home.wrapSimpleButton(Client.getUIElement("btn_trends_tab_home") as SimpleButton);
			btn_trends_tab_slave.wrapSimpleButton(Client.getUIElement("btn_trends_tab_slave") as SimpleButton);
			btn_trends_tab_lover.wrapSimpleButton(Client.getUIElement("btn_trends_tab_lover") as SimpleButton);
			btn_trends_tab_friend.wrapSimpleButton(Client.getUIElement("btn_trends_tab_friend") as SimpleButton);
			btn_trends_tab_world.wrapSimpleButton(Client.getUIElement("btn_trends_tab_world") as SimpleButton);
			btn_trends_tab_all.setText("");
			btn_trends_tab_home.setText("");
			btn_trends_tab_slave.setText("");
			btn_trends_tab_lover.setText("");
			btn_trends_tab_friend.setText("");
			btn_trends_tab_world.setText("");
			var group:ButtonGroup = new ButtonGroup();
			group.appendAll(btn_trends_tab_all,btn_trends_tab_home,btn_trends_tab_slave,btn_trends_tab_lover,btn_trends_tab_friend,btn_trends_tab_world);
			
			btn_close.wrapSimpleButton(Client.getUIElement("btn_close") as SimpleButton);
			
			txtTrends.setBackgroundDecorator(null);
			txtTrends.setText("");
			
			var filter:GlowFilter;
			var font:ASFont = new ASFont("宋体", 12, false, false, false, false);
			filter = new GlowFilter(ASColor.BLACK.getARGB(), 1, 2, 2, 10);
			txtTrends.setFont(font);
			txtTrends.filters = (new Array(filter));
			//txtTrends.filters = (new Array(new GlowFilter(0xdb9a8b, 1, 2, 2, 10)));;
			txtTrends.setForeground(ASColor.WHITE);
			Client.getInstance().addEventListener("12", phTrends_12);
			btn_trends_tab_all.addActionListener(onBtnTabAll);
			btn_trends_tab_home.addActionListener(onBtnTabHome);
			btn_trends_tab_slave.addActionListener(onBtnTabSlave);
			btn_trends_tab_lover.addActionListener(onBtnTabLover);
			btn_trends_tab_friend.addActionListener(onBtnTabFriend);
			btn_trends_tab_world.addActionListener(onBtnTabWorld);
			
			scrollpane.getVerticalScrollBar().setVisible(true);
			scrollpane.setVerticalScrollBarPolicy(JScrollPane.SCROLLBAR_ALWAYS);
			scrollpane.setHorizontalScrollBarPolicy(JScrollPane.SCROLLBAR_NEVER);
			scrollpane.getVerticalScrollBar().setUIElement(true);
			scrollpane.getVerticalScrollBar().setUI(new MyScrollBarUI);
			txtTrends.addEventListener(TextEvent.LINK, linkHandler);
		}
		
		private function linkHandler(linkEvent:TextEvent):void 
		{
			//trace("linkEvent.target:"+linkEvent.target+",linkEvent.currentTarget:"+linkEvent.currentTarget);
			var dtemp:Array = (linkEvent.text as String).split(',');
			if(dtemp.length != 5)
			{
				return;
			}
			
			var obj:Object = new Object;
			obj.m_nCharId = int(dtemp[0]);
			obj.m_szCharName = dtemp[1];
			obj.m_nLevel = int(dtemp[2]);
			obj.m_nRace = int(dtemp[3]);
			obj.m_nGender = int(dtemp[4]);
			
			//trace(linkEvent.text);
			//FrameInfo.showMessageDialog("","这是一个物品"+linkEvent.text);
			var point:IntPoint = AsWingUtils.getStageMousePosition();
			FrameClickChar._objDest = obj;
			FrameClickChar._destCharId = int(obj.m_nCharId);
			FrameClickChar.show(_frame,point.x-_frame.x,point.y-_frame.y);
			//FrameClickChar.show(0,0);
			//trace("point.x:"+point.x+" point.y:"+point.y);
		}
		static public var _arAllTrends:Array = [];
		static public  function updateAllTrends():void
		{
			_arAllTrends = [];
			for each(var trends:Array in CharInfo.m_mapListTrends)
			{
				for each(var item:Object in trends)
				{
					//textTrendsSelf += item.m_szMessage +"<br>";
					//textTrendsSelf += strLine;
					_arAllTrends.push(item);
				}
			}
			_arAllTrends.sortOn("m_tTime",Array.NUMERIC);
		}
		private var strLine:String = '<font color="#444444">----------------------------------------------------------------------------------------------------------------------</font><br>';
		private function onBtnTabAll(e:Event):void
		{
			textTrendsSelf = "";
			txtTrends.setHtmlText(textTrendsSelf);
			//for each(var trends:Array in CharInfo.m_mapListTrends)
			//{
				for each(var item:Object in _arAllTrends)
				{
					textTrendsSelf += item.m_szMessage +"<br>";
					textTrendsSelf += strLine;
				}
			//}
			txtTrends.setHtmlText(textTrendsSelf);
			txtTrends.scrollToBottomLeft();
		}
		private function onBtnTabHome(e:Event):void
		{
			textTrendsSelf = "";
			txtTrends.setHtmlText(textTrendsSelf);
			for each(var item:Object in CharInfo.m_mapListTrends[Const.TREND_TYPE_HOME])
			{
				if(int(item.m_nType) == Const.TREND_TYPE_HOME)
				{
					textTrendsSelf += item.m_szMessage +"<br>";
					textTrendsSelf += strLine;
				}
			}
			txtTrends.setHtmlText(textTrendsSelf);
			txtTrends.scrollToBottomLeft();
		}
		private function onBtnTabSlave(e:Event):void
		{
			textTrendsSelf = "";
			txtTrends.setHtmlText(textTrendsSelf);
			for each(var item:Object in CharInfo.m_mapListTrends[Const.TREND_TYPE_SLAVE])
			{
				if(int(item.m_nType) == Const.TREND_TYPE_SLAVE)
				{
					textTrendsSelf += item.m_szMessage +"<br>";
					textTrendsSelf += strLine;
				}
			}
			txtTrends.setHtmlText(textTrendsSelf);
			txtTrends.scrollToBottomLeft();
		}
		private function onBtnTabLover(e:Event):void
		{
			textTrendsSelf = "";
			txtTrends.setHtmlText(textTrendsSelf);
			for each(var item:Object in CharInfo.m_mapListTrends[Const.TREND_TYPE_LOVER])
			{
				if(int(item.m_nType) == Const.TREND_TYPE_LOVER)
				{
					textTrendsSelf += item.m_szMessage +"<br>";
					textTrendsSelf += strLine;
				}
			}
			txtTrends.setHtmlText(textTrendsSelf);
			txtTrends.scrollToBottomLeft();
		}
		private function onBtnTabFriend(e:Event):void
		{
			textTrendsFriend = "";
			txtTrends.setHtmlText(textTrendsFriend);
			for each(var item:Object in _trendsFriend)
			{
				
				textTrendsFriend += item.m_szMessage +"<br>";
				textTrendsFriend += strLine;
			}
			txtTrends.setHtmlText(textTrendsFriend);
			txtTrends.scrollToBottomLeft();
		}
		private function onBtnTabWorld(e:Event):void
		{
			textTrendsWorld = "";
			txtTrends.setHtmlText(textTrendsWorld);
			for each(var item:Object in _trendsWorld)
			{
				textTrendsWorld += item.m_szMessage +"<br>";
				textTrendsWorld += strLine;
			}
			txtTrends.setHtmlText(textTrendsWorld);
			txtTrends.scrollToBottomLeft();
		}
		static public var textTrendsSelf:String  = new String();
		static public var textTrendsFriend:String  = new String();
		static public var textTrendsWorld:String  = new String();
		static public var _trendsFriend:Array = new Array;
		static public var _trendsWorld:Array = new Array;

		static public function phTrends_12(e:PacketEvent):void
		{
			var msg:Array = e.data as Array;
			trace("phTrends_12", msg);
			var nIndex:uint = 0;
			var nType:int = msg[nIndex++];
			var nCount:uint = 0;
			var i:int = 0;
			var obj:Object = null;
			if(nType == 0)
			{	
				var nTrendType:int = msg[nIndex++];
				nCount = msg[nIndex++];
				var trends:Array = CharInfo.m_mapListTrends[nTrendType];
				trends.splice(0,trends.length);
				for(i = 0; i < nCount; i++)
				{
					obj = new Object;
					obj.m_nType = msg[nIndex++];
					obj.m_tTime = msg[nIndex++];
					obj.m_szMessage = msg[nIndex++];
					trends.push(obj);
				}
			}
			
			if(FrameChar.isShowing())
			{
				FrameChar._frame.onUpdateData();
			}
			
			if(nType == 1)
			{	
				nCount = msg[nIndex++];
				_trendsFriend.splice(0,_trendsFriend.length);
				for(i = 0; i < nCount; i++)
				{
					obj = new Object;
					obj.m_nType = msg[nIndex++];
					obj.m_tTime = msg[nIndex++];
					obj.m_szMessage = msg[nIndex++];
					_trendsFriend.push(obj);
				}
			}
			if(nType == 2)
			{	
				nCount = msg[nIndex++];
				_trendsWorld.splice(0,_trendsWorld.length);
				for(i = 0; i < nCount; i++)
				{
					obj = new Object;
					obj.m_nType = msg[nIndex++];
					obj.m_tTime = msg[nIndex++];
					obj.m_szMessage = msg[nIndex++];
					_trendsWorld.push(obj);
				}
			}
			updateAllTrends();
			if(isShowing())
			{
				if(_frame.btn_trends_tab_all.isSelected())
					_frame.onBtnTabAll(null);
				
				if(_frame.btn_trends_tab_friend.isSelected())
				{
					if(_trendsFriend.length == 0)
					{
						_frame.btn_trends_tab_friend.setSelected(false);
						_frame.btn_trends_tab_world.setSelected(true);
						_frame.onBtnTabWorld(null);
						return;
					}
					_frame.onBtnTabFriend(null);
				}
				if(_frame.btn_trends_tab_world.isSelected())
					_frame.onBtnTabWorld(null);
			}
			if(FrameLover.isShowing())
				FrameLover._frame.update();
		}
		public static var _firstShow:Boolean = false;
		private static var _frame:FrameTrends = null;
		public static function show():void
		{
			if(isShowing())
			{
				hide();
				return;
			}
			if(_frame == null)
			{
				_frame = new FrameTrends();
				PanelMainHelp.getInstance().addChild(_frame);
			}
			_frame.show();
			PanelMainHelp.getInstance().bringToTop(_frame);
			_frame.onBtnTabAll(null);
			Client.getInstance().sendPacketLogic(12,[1,0]);
			Client.getInstance().sendPacketLogic(12,[2,0]);

			_frame.x = (Engine.W - _frame.width) / 2;
			_frame.y = (Engine.H - _frame.height) / 2;
			
		}
		public static function hide():void
		{
			if(isShowing())_frame.hide();
		}
		public static function isShowing():Boolean
		{
			return(_frame != null&& _frame.isShowing());
		}
	}
}